package com.contacts.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.contacts.pojo.User;
import com.contacts.service.UserService;


public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		User u = new User();

		try {
			BeanUtils.populate(u,request.getParameterMap());
				//登录操作
				UserService us=new UserService();
				User user=us.findUserByLogin(u);
				if(user!=null){
					//登录成功
					request.getSession().setAttribute("user", user);
					response.sendRedirect(request.getContextPath()+"/findall");
				}else{
					request.setAttribute("msg", "账号或者密码错误");
					request.getRequestDispatcher("/login.jsp").forward(request, response);
					return;
				}
		} catch (Exception e) {
			request.setAttribute("msg", "服务器异常");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			e.printStackTrace();
		}
}
		
	

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
